package com.valuegroup.smarttodo.ui.programme.goals;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.data.ServiceFactory;
import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.Goals;
import com.valuegroup.smarttodo.data.model.Remark;
import com.valuegroup.smarttodo.ui.BaseActivity;
import com.valuegroup.smarttodo.plugins.MultilineEditText;
import com.valuegroup.smarttodo.plugins.pickdialog.DateUtils;
import com.valuegroup.smarttodo.util.AlertUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 任务详情备注
 */
public class GoalsRemarkActivity extends BaseActivity {

    @BindView(R.id.labDate)
    TextView labDate;
    @BindView(R.id.editContent)
    MultilineEditText editContent;

    private Long DATA_ID;
    private Integer DATA_POSITION;
    private Goals DATA;
    private Remark remark = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_taskdetail_remark);
        ButterKnife.bind(this);

        DATA_ID = getIntent().getLongExtra(PARAMS_KEY, 0);
        DATA_POSITION = getIntent().getIntExtra(PARAMS_POSITION, 0);
        DATA = ServiceFactory.getInstance().getGoalsService().getGoalsDao().load(DATA_ID);

        initData();


        /**
         * 动态给EditText获得焦点并弹起软键盘
         */
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        editContent.setFocusable(true);
        editContent.setFocusableInTouchMode(true);
        editContent.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editContent,0);
    }

    private void initData() {
        if (DATA_ID != 0) {
            remark = ServiceFactory.getInstance().getRemarkService().getRemark(AppStatus.RemarkType.GOALS, DATA_ID);
            if (remark != null) {
                labDate.setText(String.format(getResources().getString(R.string.update_time), DateUtils.date2str(remark.getUpdatedDt(), DateUtils.F33)));
                labDate.setVisibility(View.VISIBLE);
                editContent.setText(remark.getContent());
            } else {
                labDate.setText("");
                editContent.setText("");
                labDate.setVisibility(View.GONE);
            }
        }
    }

    @OnClick({R.id.btnBack, R.id.btnSave, R.id.btnClear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finishAfterTransition();
                break;
            case R.id.btnClear:
                AlertUtils.dialog(this, getResources().getString(R.string.alt_clear_remark),
                        getResources().getString(R.string.op_determine),
                        getResources().getString(R.string.op_cancel), (dialog, which) -> {
                            ServiceFactory.getInstance().getRemarkService().clearRemark(AppStatus.RemarkType.GOALS, DATA_ID);
                            initData();
                        }, null);
                break;
            case R.id.btnSave:
                if (!TextUtils.isEmpty(editContent.getText())) {
                    remark = ServiceFactory.getInstance().getRemarkService().saveRemark(AppStatus.RemarkType.GOALS, DATA_ID, editContent.getText().toString());
                }
                finishAfterTransition();
                break;
        }
    }
}
